//
//  KDInboxController.m
//  WangJingApp
//
//  Created by 魏强 on 17/1/23.
//  Copyright © 2017年 KDTech. All rights reserved.
//  收件箱

#import "KDInboxController.h"
#import "KDNotyCell.h"
#import "KDNotyCell1.h"
#import "SVProgressHUD.h"
#import "KDNotyRequestTool.h"
#import "UITableView+FDTemplateLayoutCell.h"
#import "KDSignController.h"
#import "KDNavigationController.h"
#import "KDDetailNotyController.h"
@interface KDInboxController ()<KDNotyCellDelegate,KDNotyCell1Delegate>
/**  <#Description#> */
@property (nonatomic, strong) NSArray *infoArr;
/**
 *  加载第几页
 */
@property (nonatomic, copy) NSString *page;
/**  <#Description#> */
@property (nonatomic, copy) NSString * cellId;

@end

@implementation KDInboxController
#pragma mark - 懒加载区
- (NSArray *)infoArr
{
    if (!_infoArr) {
        _infoArr = [[NSArray alloc]init] ;
    }
    return _infoArr;
}
#pragma mark - 生命周期区
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTableView];
    
}
#pragma mark - 板块布置区
- (void)setupTableView{
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
    self.cellId =(self.type.integerValue)?[NSString stringWithFormat:@"KDNotyCell%@",self.type]:@"KDNotyCell";
    UINib *xib = [UINib nibWithNibName:self.cellId bundle:nil];
    self.tableView.tag = self.type.integerValue;
    
    
    [self.tableView registerNib:xib forCellReuseIdentifier:self.cellId];
    self.tableView.separatorColor = WQColor(245, 245, 245);
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    // 设置footer
    self.tableView.footer = footer;
    [self.tableView.header beginRefreshing];
}
#pragma mark - 事件点击区

#pragma mark - 网络请求区
- (void)loadData{
    self.page = @"1";
    KDNotyBoxParam *param = [[KDNotyBoxParam alloc]init];
    param.method = (self.type.integerValue)?@"message/outBox":@"message/receivedMsg";
//    param.isSignIn = self.type;
    param.pageNo = self.page;
    param.pageSize = @"10";
    [KDNotyRequestTool filterMsgWithParam:param success:^(KDNotyBoxResult *responseObejct) {
        if (responseObejct.status != 1) {
            [SVProgressHUD showErrorWithStatus:@"请求错误" maskType:SVProgressHUDMaskTypeGradient];
        }else{
            NSDictionary *dic = responseObejct.data;
            NSArray *arr = dic[@"list"];
            self.infoArr = [KDNotyCellModel objectArrayWithKeyValuesArray:arr];
        }
        [self.tableView.header endRefreshing];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请求错误" maskType:SVProgressHUDMaskTypeGradient];
        [self.tableView.header endRefreshing];
    }];
}
- (void)loadMoreData{
    self.page = [NSString stringWithFormat:@"%ld",(long)self.page.integerValue + 1] ;
    KDNotyBoxParam *param = [[KDNotyBoxParam alloc]init];
    param.method = (self.type.integerValue)?@"message/outBox":@"message/receivedMsg";
    //    param.isSignIn = self.type;
    param.pageNo = self.page;
    param.pageSize = @"10";
    [KDNotyRequestTool filterMsgWithParam:param success:^(KDNotyBoxResult *responseObejct) {
        if (responseObejct.status != 1) {
            [SVProgressHUD showErrorWithStatus:@"请求错误" maskType:SVProgressHUDMaskTypeGradient];
            [self.tableView.footer endRefreshing];
        }else{
            NSDictionary *dic = responseObejct.data;
            NSArray *arr = dic[@"list"];
            if (arr.count) {
                NSMutableArray *muteArr = [NSMutableArray arrayWithArray:self.infoArr];
                arr = [KDNotyCellModel objectArrayWithKeyValuesArray:arr];
                [muteArr addObjectsFromArray:arr];
                self.infoArr = muteArr;
                [self.tableView.footer endRefreshing];
                [self.tableView reloadData];

            }else{//
                [self.tableView.footer noticeNoMoreData];
            }
            

        }
    } failure:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"请求错误" maskType:SVProgressHUDMaskTypeGradient];
        [self.tableView.footer endRefreshing];
    }];
}
#pragma mark - 通知接收区


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.infoArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.type.integerValue) {
        KDNotyCell1 *cell = [KDNotyCell1 cellWithTableView:tableView];
        cell.delegate = self;
        if (self.infoArr.count != 0) {
            cell.model = self.infoArr[indexPath.row];
        }
        return cell;
    }else{
        KDNotyCell *cell = [KDNotyCell cellWithTableView:tableView];
        cell.delegate = self;
        if (self.infoArr.count != 0) {
            cell.model = self.infoArr[indexPath.row];
        }
        return cell;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [tableView fd_heightForCellWithIdentifier:self.cellId configuration:^(KDNotyCell * cell) {
        // Configure this cell with data, same as what you've done in "-tableView:cellForRowAtIndexPath:"
        // Like:
        cell.delegate = self;
        cell.model = self.infoArr[indexPath.row];
    }];
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!self.type.integerValue){
        KDNotyCellModel *model = self.infoArr[indexPath.row];
        return (model.isSigned)?indexPath:nil;
        
    }else{
        return indexPath;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KDDetailNotyController *detail = [[KDDetailNotyController alloc]init];
    [self.navigationController pushViewController:detail animated:YES];
}
#pragma mark- KDNotyCellDelegate
- (void)pushTosignPageWithType:(BOOL)isSigned{
    KDSignController *sign = [[KDSignController alloc]init];
    sign.isSigned = isSigned;
    [self.navigationController pushViewController:sign animated:YES];
}
- (void)KDNotyCell:(KDNotyCell *)cell signBtnClick:(UIButton *)signnBtn{
    KDLog(@"signBtnClick" );
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
//    KDNotyCellModel *model = self.infoArr[indexPath.row];
    
    KDLog(@"row:%ld   section:%ld   inforCount:%ld"   ,(long)indexPath.row,(long)indexPath.section, self.infoArr.count);
    NSMutableArray *arr = [NSMutableArray arrayWithArray:self.infoArr];
    [arr replaceObjectAtIndex:indexPath.row withObject:cell.model];
    self.infoArr = arr;
}
- (void)KDNotyCell1:(KDNotyCell1 *)cell unsignedBtnClick:(UIButton *)unsignedBtn{
    [self pushTosignPageWithType:NO];
}
- (void)KDNotyCell1:(KDNotyCell1 *)cell signedBtnClick:(UIButton *)signedBtn{
    [self pushTosignPageWithType:YES];
}
- (void)KDNotyCell1:(KDNotyCell1 *)cell supervisionBtnClick:(UIButton *)supervisionBtn{
    KDLog(@"supervisionBtnClick" );
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
